package com.hbpu.usercenter.controller;

import com.hbpu.usercenter.common.BaseResponse;
import com.hbpu.usercenter.common.ErrorCode;
import com.hbpu.usercenter.common.ResultUtils;
import com.hbpu.usercenter.dto.DeleteTeamDTO;
import com.hbpu.usercenter.dto.JoinTeamDTO;
import com.hbpu.usercenter.dto.QuitTeamDTO;
import com.hbpu.usercenter.dto.TeamDTO;
import com.hbpu.usercenter.exception.BusinessException;
import com.hbpu.usercenter.pojo.Team;
import com.hbpu.usercenter.request.*;
import com.hbpu.usercenter.service.TeamService;
import com.hbpu.usercenter.service.UserService;
import com.hbpu.usercenter.vo.TeamUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 负责前端Team相关请求的控制层
 */
@Tag(name = "负责前端Team相关请求的控制层", description = "负责前端Team相关请求的控制层")
@RestController
@RequestMapping("/team")
//@CrossOrigin(origins = "http://192.168.1.192:4173", allowCredentials = "true")
@Slf4j
public class TeamController {
    private final UserService userService;
    private final TeamService teamService;


    @Autowired
    public TeamController(UserService userService, TeamService teamService) {
        this.userService = userService;
        this.teamService = teamService;
    }

    /**
     * 添加队伍接口
     * @param addTeamRequest 添加队伍所需信息的封装对象
     * @param request 用户请求
     * @return 添加成功后队伍的id
     */
    @Operation(summary = "添加队伍接口", description = "添加队伍接口")
    @PostMapping("/add")
    public BaseResponse<Long> addTeam(@RequestBody AddTeamRequest addTeamRequest, HttpServletRequest request) {
        if (addTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TeamDTO teamDTO = new TeamDTO();
        BeanUtils.copyProperties(addTeamRequest, teamDTO);
        teamDTO.setUserId(userService.getCurrentUser(request).getId());
        return ResultUtils.success(teamService.addTeam(teamDTO,  request));
    }

    /**
     * 更新队伍信息的接口
     * @param updateTeamRequest 用于接收前端队伍更新信息的封装对象
     * @param request 用户请求
     * @return 更新成功返回true，否则抛异常
     */
    @Operation(summary = "更新队伍信息的接口", description = "更新队伍信息的接口")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody UpdateTeamRequest updateTeamRequest, HttpServletRequest request) {
        if (updateTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TeamDTO teamDTO = new TeamDTO();
        BeanUtils.copyProperties(updateTeamRequest, teamDTO);
        Boolean updateResult = teamService.updateTeam(teamDTO, request);
        if (!updateResult) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败！");
        }
        return ResultUtils.success(true);
    }

    /**
     * 根据队伍id查询队伍
     * @param id 队伍id
     * @return 返回一个返回给前端的队伍封装对象
     */
    @Parameter(name = "id", description = "队伍id", in = ParameterIn.PATH, required = true)
    @Operation(summary = "根据队伍id查询队伍", description = "根据队伍id查询队伍")
    @GetMapping("/select/{id}")
    public BaseResponse<TeamDTO> selectTeamById(@PathVariable long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Team team = teamService.getById(id);
        if (team == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        TeamDTO teamDTO = new TeamDTO();
        BeanUtils.copyProperties(team, teamDTO);
        return ResultUtils.success(teamDTO);
    }

    /**
     * 查询所有队伍的接口
     * @param request 用户请求
     * @return 所有队伍封装对象的列表
     */
    @Operation(summary = "查询所有队伍的接口", description = "查询所有队伍的接口")
    @GetMapping("/list")
    public BaseResponse<List<TeamUserVO>> selectAllTeams(HttpServletRequest request) {
        if (!userService.isLogin(request)) {
            throw  new BusinessException(ErrorCode.NOT_LOGIN);
        }
        List<TeamUserVO> teamUserVOS = teamService.listTeam();
        return ResultUtils.success(teamUserVOS);
    }

    /**
     * 加入队伍的接口
     * @param joinTeamRequest 用户接收前端加入队伍数据的请求的包装对象
     * @param request 用户请求
     * @return 加入成功返回true
     */
    @Operation(summary = "加入队伍的接口", description = "加入队伍的接口")
    @PostMapping("/join")
    public BaseResponse<Boolean> joinTeam(@RequestBody JoinTeamRequest joinTeamRequest, HttpServletRequest request) {
        if (joinTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        JoinTeamDTO joinTeamDTO = new JoinTeamDTO();
        BeanUtils.copyProperties(joinTeamRequest, joinTeamDTO);
        joinTeamDTO.setUserId(userService.getCurrentUser(request).getId());
        return ResultUtils.success(teamService.joinTeam(joinTeamDTO));
    }

    /**
     * 退出队伍的接口
     * @param quitTeamRequest 用于接收退出队伍数据的请求体
     * @param request 用户请求
     * @return 退出成功返回true
     */
    @Operation(summary = "退出队伍的接口", description = "用于处理退出队伍的接口")
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(@RequestBody QuitTeamRequest quitTeamRequest, HttpServletRequest request) {
        if (quitTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QuitTeamDTO quitTeamDTO = new QuitTeamDTO();
        BeanUtils.copyProperties(quitTeamRequest, quitTeamDTO);
        quitTeamDTO.setUserId(userService.getCurrentUser(request).getId());
        return ResultUtils.success(teamService.quitTeam(quitTeamDTO));
    }

    /**
     * 删除队伍的接口
     * @param deleteTeamRequest 接收删除队伍数据的请求体
     * @param request 用户请求
     * @return 删除成功返回true
     */
    @Operation(summary = "删除队伍的接口", description = "删除队伍的接口")
    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteTeam(@RequestBody DeleteTeamRequest deleteTeamRequest, HttpServletRequest request) {
        if (deleteTeamRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        DeleteTeamDTO deleteTeamDTO = new DeleteTeamDTO();
        BeanUtils.copyProperties(deleteTeamRequest, deleteTeamDTO);
        deleteTeamDTO.setUserId(userService.getCurrentUser(request).getId());
        return ResultUtils.success(teamService.deleteTeam(deleteTeamDTO));
    }
}
